<?php
namespace app\api\controller\v1;

use think\Db;
use \think\Controller;

class BaseController extends Controller{

	protected $lang =[];

	

	function _initialize(){
		//语言包 不使用框架behavior
		$clientlang = request()->header('lang');
		if(empty($clientlang)){
			$clientlang = input('param.lang/s');
		}
		if(in_array($clientlang, array('zh_cn','en_us'))){
			$lan = $clientlang;
		}else{
			$lan= 'zh_cn';
		}
		$lan = str_replace('_','-',$lan);
		$this->lang= include(CMF_ROOT.'system/cmf/lang/'.$lan.'.php');	
	}

	/**
	 * [checkToken description]
	 * @Author   思睿软件
	 * @DateTime 2021-07-13T14:27:39+0800
	 * @Version  1.0
	 * @param    string                   $token [description]
	 * @return   [type]                          [description]
	 * 根据token返回用户资料
	 */
	protected function checkToken($token=''){
		if(empty($token)){
			$token = request()->header('token');
		}
		if(empty($token)){
			$token = input('param.token/s');
		}
		if(empty($token)){
			return 0;
		}
		$condition['ut.token']      = $token;
		$condition['u.user_status'] =1;
		$condition['ut.expire_time']= ['>',time()];
		$row = Db::name('user_token')
		       ->alias('ut')
		       ->where($condition)
		       ->join('user u','ut.user_id=u.id','left')
		       ->field('u.*')
		       ->find();
		return intval($row['id']);
	}



	//根据token 获取资料
	protected function getInfoByToken($token=''){
		if(empty($token)){
			$token = request()->header('token');
		}
		if(empty($token)){
			$token = input('param.token/s');
		}
		if(empty($token)){
			return [];
		}
		$condition['ut.token']      = $token;
		$condition['ut.expire_time']= ['>',time()];
		$row = Db::name('user_token')
		       ->alias('ut')
		       ->where($condition)
		       ->join('user u','ut.user_id=u.id','left')
		       ->field('u.*,ut.token')
		       ->find();
		//print_r($row);
		return empty($row) ? [] :$row;
	}



	
	protected function deal_user($row){
		$all_levels = model('UserLevel')->getAll();
		$level = get_one_by_item($all_levels,'level',$row['level']);
		$ret=[];
		//$ret['id']                 = $row['id'];
		$ret['nickname']           = $row['user_nickname'];
		$ret['user_login']         = $row['user_login'];
		$ret['user_email']         = $row['user_email'];
		$ret['mobile']             = $row['mobile'];
		$ret['amount']             = $row['amount'];
		// $ret['score']              = $row['score'];
		// $ret['coin']               = $row['coin'];
		// $ret['parent_user_id']     = $row['parent_user_id'];
		$ret['register_at']        = date('Y-m-d H:i:s',$row['create_time']);
		$ret['level']              = $row['level'];
		$ret['level_title']        = $level['title'];
		$ret['group_num']          = $row['group_num'];
		$ret['avatar']             = $row['avatar']? request()->domain().'/'.$row['avatar'] : request()->domain().'/'.config('default_face');
		return $ret;
	}


	


	protected function mul_deal_user($rows=array()){
		if(empty($rows)) return [];
		$rets=[];
		foreach ($rows as $key => $value) {
			$row = $this->deal_user($value);
			$rets[]= $row;
		}
		return $rets;
	}
}